﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SDWXServer.Device
{
    public class SDMqttMessage
    {
        public SDMqttMessage()
        {
            this.Id = Guid.NewGuid().ToString();
            this.Params = new Kingdee.BOS.JSON.JSONObject();
        }
        public string Id { get; set; }
        public string Method { get; set; }
        public string DeviceId { get; set; }
        public Kingdee.BOS.JSON.JSONObject Params { get; set; }
        public override string ToString()
        {
            var ret = new Kingdee.BOS.JSON.JSONObject();
            ret["Id"] = this.Id;
            ret["Method"] = this.Method;
            ret["DeviceId"] = this.DeviceId;
            ret["Params"] = this.Params;

            return ret.ToString();
        }

        public static SDMqttMessage Parse(string data)
        {
            var jsonData = Kingdee.BOS.JSON.JSONObject.Parse(data, null);
            if (jsonData == null) return null;

            var ret = new SDMqttMessage();
            ret.Id = jsonData.GetValue<string>("Id", "");
            ret.Method = jsonData.GetValue<string>("Method", "");
            ret.DeviceId = jsonData.GetValue<string>("DeviceId", "");
            ret.Params = jsonData.GetValue<Kingdee.BOS.JSON.JSONObject>("Params", new Kingdee.BOS.JSON.JSONObject());

            return ret;
        }
    }
}
